package com.example.teacher;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.example.adapter.AllCourseAdapter;
import com.example.adapter.CourseAdapter;
import com.example.adapter.CourseOptionAdapter;
import com.example.database.DatabaseHelper;
import com.example.pojo.Course;
import com.example.pojo.CourseOption;
import com.example.student.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CoursesFragment extends Fragment {
    private Map<String, List<String>> collegeMajorMap = new HashMap<>();

    private Spinner spinnerCollege, spinnerMajor,spinnerCourseType;
    private RecyclerView recyclerViewCourses;
    private CourseOptionAdapter courseAdapter;
    private List<CourseOption> courseOptionList = new ArrayList<>();

    private DatabaseHelper databaseHelper;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_courses, container, false);
        // 初始化数据库
        databaseHelper = new DatabaseHelper(getContext());
        // 初始化控件
        spinnerCollege = view.findViewById(R.id.spinnerCollege);
        spinnerMajor = view.findViewById(R.id.spinnerMajor);
        spinnerCourseType = view.findViewById(R.id.spinnerCourseType);
        recyclerViewCourses = view.findViewById(R.id.recyclerViewCourses);
        // 设置 RecyclerView
        recyclerViewCourses.setLayoutManager(new LinearLayoutManager(getContext()));
        courseAdapter = new CourseOptionAdapter(courseOptionList, getContext(), databaseHelper);
        recyclerViewCourses.setAdapter(courseAdapter);




        initSpinnerData();
        // 初始化课程列表
        initCourseList();

        // 监听 Spinner 的选择事件
        setupSpinnerListeners();
//        Log.d("aaa", "onCreateView: courseOptionList" + courseOptionList);
        return view;
    }



    private void initSpinnerData() {
        // 初始化学院和专业数据
        collegeMajorMap = new HashMap<>();
        collegeMajorMap.put("计算机科学与技术", List.of("物联网工程", "计算机科学与技术", "网络安全"));
        collegeMajorMap.put("商学院", List.of("工商管理", "市场营销", "会计学"));
        collegeMajorMap.put("文学院", List.of("汉语言文学", "新闻学"));
        collegeMajorMap.put("理学院", List.of("数学与应用数学", "物理学", "化学"));
        collegeMajorMap.put("工程学院", List.of("机械工程", "电气工程", "土木工程"));
        collegeMajorMap.put("医学院", List.of("临床医学", "口腔医学", "药学"));
        collegeMajorMap.put("艺术学院", List.of("音乐学", "美术学", "设计学"));
        collegeMajorMap.put("法学院", List.of("法学", "知识产权", "国际法"));
        collegeMajorMap.put("教育学院", List.of("教育学", "心理学", "学前教育"));
        collegeMajorMap.put("农学院", List.of("农学", "园艺学", "植物保护"));

        // 初始化学院下拉框
        List<String> colleges = new ArrayList<>(collegeMajorMap.keySet());
        colleges.add(0, "请选择学院"); // 添加“请选择”选项
        ArrayAdapter<String> collegeAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, colleges);
        collegeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCollege.setAdapter(collegeAdapter);

        // 初始化专业下拉框
        List<String> majors = new ArrayList<>();
        majors.add("请选择专业"); // 添加“请选择”选项
        ArrayAdapter<String> majorAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, majors);
        majorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMajor.setAdapter(majorAdapter);

        // 初始化课程类型下拉框
        List<String> courseTypes = new ArrayList<>();
        courseTypes.add("请选择课程类型"); // 添加“请选择”选项
        courseTypes.addAll(List.of("必修", "选修"));
        ArrayAdapter<String> courseTypeAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, courseTypes);
        courseTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCourseType.setAdapter(courseTypeAdapter);
    }

    private void initCourseList() {
        // 从数据库获取课程信息
        List<CourseOption> courses = databaseHelper.getCourseOptions();
        courseOptionList.addAll(courses);
        courseAdapter.notifyDataSetChanged();
    }

    private void setupSpinnerListeners() {
        spinnerCollege.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedCollege = (String) parent.getItemAtPosition(position);
                updateMajorSpinner(selectedCollege);
                filterCourses(selectedCollege, (String) spinnerMajor.getSelectedItem(), (String) spinnerCourseType.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        spinnerMajor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedMajor = (String) parent.getItemAtPosition(position);
                filterCourses((String) spinnerCollege.getSelectedItem(), selectedMajor, (String) spinnerCourseType.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        spinnerCourseType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedCourseType = (String) parent.getItemAtPosition(position);
                filterCourses((String) spinnerCollege.getSelectedItem(), (String) spinnerMajor.getSelectedItem(), selectedCourseType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private void filterCourses(String college, String major, String courseType) {
        // 如果选择的是“请选择”，则清空条件
        if ("请选择学院".equals(college)) {
            college = "";
        }
        if ("请选择专业".equals(major)) {
            major = "";
        }
        if ("请选择课程类型".equals(courseType)) {
            courseType = "";
        }

        // 从数据库获取符合条件的课程
        courseOptionList.clear();
        courseOptionList.addAll(databaseHelper.getCourseOptionsByFilter(college, major, courseType));
        courseAdapter.notifyDataSetChanged();
    }

    private void updateMajorSpinner(String college) {
        // 获取专业列表
        List<String> majors = collegeMajorMap.get(college);
        if (majors == null) {
            majors = new ArrayList<>(); // 如果 majors 为 null，使用空列表
        }

        // 添加“请选择专业”选项
        List<String> majorList = new ArrayList<>();
        majorList.add("请选择专业"); // 添加“请选择”选项
        majorList.addAll(majors);

        // 设置适配器
        ArrayAdapter<String> majorAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, majorList);
        majorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMajor.setAdapter(majorAdapter);

        // 设置默认选中第一项
        if (majorAdapter.getCount() > 0) {
            spinnerMajor.setSelection(0);
        }
    }

}